<?php

declare(strict_types=1);

namespace App\Util;

class ComUtil
{
    static function checkDevEnv()
    {
        return env('APP_DEBUG') == true;
    }

    /**
     * 图片构建
     */
    static function buidlImgaeHtml($picLinks = [], $width = 200, $height = 200, $newHost = false)
    {
        if (!$picLinks) return '';
        if (!is_array(($picLinks))) $picLinks = [$picLinks];
        $html = '';
        $host = $newHost ? AliyunUtil::IMG_HOST_V2 : AliyunUtil::IMG_HOST;
        foreach ($picLinks as $picLink) {
            $pic = strpos($picLink, 'http') !== false ? $picLink : AliyunUtil::imageResize($host . $picLink, $width, $height);
            $bigPic = strpos($picLink, 'http') !== false ? $picLink : $host . $picLink;
            $img = "<img src='$pic' style='max-width:100%;'>";
            $img = '<a href="' . $bigPic . '"  data-lightbox="image-1" rel="lightbox" title="my caption">' . $img . '</a>';
            $html .= "<div style='width:150px;height:150px;overflow:hidden;'>$img</div><br/>";
        }
        return $html;
    }

    static function buildAudio($link = '', $type = 'audio')
    {
        $link = strpos($link, 'http') !== false ? $link : AliyunUtil::FILE_HOST . $link;
        if ($type == 'audio') {
            return '<audio controls>
            <source src="' . $link . '" type="audio/aac">
            您的浏览器不支持 audio 元素。
        </audio>';
        } else {
            return '<video width="320" height="240" controls="controls">
            <source src="' . $link . '" type="video/ogg" />
            <source src="' . $link . '" type="video/mp4" />
                            您的浏览器不支持 video 标签。
        </video>';
        }
    }

    /**
     * 重写实现 http_build_query 提交实现(同名key)key=val1&key=val2
     * @param array $formData 数据数组
     * @param string $numericPrefix 数字索引时附加的Key前缀
     * @param string $argSeparator 参数分隔符(默认为&)
     * @param string $prefixKey Key 数组参数，实现同名方式调用接口
     * @return string
     */
    static function buildQuery($formData, $numericPrefix = '', $argSeparator = '&', $prefixKey = '')
    {
        $str = '';
        foreach ($formData as $key => $val) {
            if (!is_array($val)) {
                $str .= $argSeparator;
                if ($prefixKey === '') {
                    if (is_int($key)) {
                        $str .= $numericPrefix;
                    }
                    $str .= urlencode($key) . '=' . urlencode($val);
                } else {
                    $str .= urlencode($prefixKey) . '=' . urlencode($val);
                }
            } else {
                if ($prefixKey == '') {
                    $prefixKey .= $key;
                }
                if (is_array($val[0])) {
                    $arr = array();
                    $arr[$key] = $val[0];
                    $str .= $argSeparator . http_build_query($arr);
                } else {
                    $str .= $argSeparator . self::buildQuery($val, $numericPrefix, $argSeparator, $prefixKey);
                }
                $prefixKey = '';
            }
        }
        return substr($str, strlen($argSeparator));
    }

    /**
     * 发起 server 请求
     * @param $action
     * @param $params
     * @param $httpHeader
     * @return mixed
     */
    static function httpReq($action, $params, $contentType = 'urlencoded', $httpMethod = 'POST', $headers = [])
    {
        $httpHeader = $headers;
        $ch = curl_init();
        if ($httpMethod == 'POST' && $contentType == 'urlencoded') {
            $httpHeader[] = 'Content-Type:application/x-www-form-urlencoded';
            curl_setopt($ch, CURLOPT_POSTFIELDS, self::buildQuery($params));
        }
        if ($httpMethod == 'POST' && $contentType == 'json') {
            $httpHeader[] = 'Content-Type:Application/json';
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        }
        if ($httpMethod == 'GET' && $contentType == 'urlencoded') {
            $action .= strpos($action, '?') === false ? '?' : '&';
            $action .= self::buildQuery($params);
        }
        curl_setopt($ch, CURLOPT_URL, $action);
        curl_setopt($ch, CURLOPT_POST, $httpMethod == 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //处理http证书问题
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $ret = curl_exec($ch);
        if (false === $ret) {
            $ret = curl_errno($ch);
        }
        curl_close($ch);
        return $ret;
    }

    static function buildOrderId($key = '')
    {
        return time() . "_{$key}_" . mt_rand(1000, 9999);
    }

    static function buildSandOrderId($key = '')
    {
        return date('YmdHis', time()) . $key;
    }

    static function buildRequestId()
    {
        return time() . '_' . mt_rand(1000000, 9999999);
    }
}
